// marketing.js
Page({
  data: {
    time: '12:30',
    currentTab: 'all',
    currentBanner: 0,
    statistics: {
      activeCount: 5,
      participantCount: 2154,
      couponUsedCount: 86,
      totalRevenue: 42600
    },
    banners: [
      {
        id: 1,
        title: '春季婚礼特惠活动',
        description: '全场婚纱礼服8折起，新人专享多重好礼',
        image: '/assets/images/banners/spring_wedding.jpg'
      },
      {
        id: 2,
        title: '夏季礼服新品发布',
        description: '2024夏季新款礼服，预约参与可获专属折扣',
        image: '/assets/images/banners/summer_collection.jpg'
      },
      {
        id: 3,
        title: '摄影套餐优惠',
        description: '精选婚纱摄影套餐，限时85折优惠',
        image: '/assets/images/banners/photography.jpg'
      }
    ],
    campaigns: [
      {
        id: 1,
        title: '春季婚礼特惠活动',
        dateRange: '2024-03-15 至 2024-04-15',
        description: '全场婚纱礼服8折起，新人专享多重好礼，活动期间下单立享免费试纱服务。',
        statusText: '进行中',
        statusClass: 'active',
        image: '/assets/images/campaigns/spring_wedding.jpg',
        stats: {
          views: 1235,
          participants: 326,
          orders: 42,
          revenue: '¥27,600'
        }
      },
      {
        id: 2,
        title: '新品礼服发布会',
        dateRange: '2024-04-20 至 2024-04-22',
        description: '现场发布2024夏季新款礼服，预约参与可获得专属定制服务折扣。',
        statusText: '即将开始',
        statusClass: 'upcoming',
        image: '/assets/images/campaigns/new_collection.jpg',
        stats: {
          views: 876,
          participants: 0,
          orders: 32,
          revenue: '¥0'
        }
      },
      {
        id: 3,
        title: '情人节特别活动',
        dateRange: '2024-02-10 至 2024-02-14',
        description: '情人节特别活动，预约拍摄套餐享受8.5折优惠，送精美相框。',
        statusText: '已结束',
        statusClass: 'ended',
        image: '/assets/images/campaigns/valentines_day.jpg',
        stats: {
          views: 1562,
          participants: 412,
          orders: 53,
          revenue: '¥15,000'
        }
      }
    ],
    coupons: [
      {
        id: 1,
        title: '婚纱套餐优惠券',
        amount: '¥300',
        restriction: '满3000使用',
        validUntil: '2024-05-31',
        issued: 500,
        used: 120
      },
      {
        id: 2,
        title: '摄影服务折扣券',
        amount: '85折',
        restriction: '无门槛使用',
        validUntil: '2024-04-30',
        issued: 300,
        used: 86
      },
      {
        id: 3,
        title: '精美相册赠送券',
        amount: '赠礼',
        restriction: '满5000使用',
        validUntil: '2024-06-30',
        issued: 200,
        used: 45
      }
    ],
    channels: [
      {
        id: 1,
        title: '微信公众号',
        type: 'wechat',
        icon: 'icon-wechat',
        stats: '本月发文：8篇 / 平均阅读：1,568',
        followers: '16,723'
      },
      {
        id: 2,
        title: '抖音号',
        type: 'douyin',
        icon: 'icon-music',
        stats: '本月发布：12条 / 平均点赞：3,245',
        followers: '28,506'
      },
      {
        id: 3,
        title: '微博',
        type: 'weibo',
        icon: 'icon-weibo',
        stats: '本月发文：15条 / 平均转发：245',
        followers: '8,932'
      },
      {
        id: 4,
        title: '线下活动',
        type: 'offline',
        icon: 'icon-store',
        stats: '本月活动：2场 / 平均参与：85人',
        followers: '-'
      }
    ]
  },

  onLoad: function() {
    this.updateTime();
    this.getTimerID = setInterval(this.updateTime, 60000);
    
    // 设置轮播图切换
    this.bannerTimer = setInterval(() => {
      let current = this.data.currentBanner;
      current = (current + 1) % this.data.banners.length;
      this.setData({
        currentBanner: current
      });
    }, 3000);
  },
  
  onUnload: function() {
    clearInterval(this.getTimerID);
    clearInterval(this.bannerTimer);
  },
  
  updateTime: function() {
    const date = new Date();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    this.setData({
      time: `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
    });
  },
  
  onSearchInput: function(e) {
    const keyword = e.detail.value;
    // 实际项目中这里可以实现搜索功能
    console.log('搜索关键词:', keyword);
  },
  
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentTab: tab
    });
    
    // 实际项目中这里应该根据选中的标签筛选数据
    this.filterCampaignsByTab(tab);
  },
  
  filterCampaignsByTab: function(tab) {
    // 这里应该实现根据标签筛选活动的逻辑
    console.log('切换到标签:', tab);
    
    // 示例代码：
    /*
    wx.request({
      url: 'https://api.example.com/campaigns',
      data: { 
        status: tab === 'all' ? '' : tab 
      },
      success: (res) => {
        this.setData({
          campaigns: res.data
        });
      }
    });
    */
  },
  
  viewAllCampaigns: function() {
    wx.showToast({
      title: '查看全部活动',
      icon: 'none'
    });
  },
  
  viewAllCoupons: function() {
    wx.showToast({
      title: '查看全部优惠券',
      icon: 'none'
    });
  },
  
  viewAllChannels: function() {
    wx.showToast({
      title: '查看全部营销渠道',
      icon: 'none'
    });
  },
  
  analyzeCampaign: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: `分析活动ID: ${id}`,
      icon: 'none'
    });
  },
  
  editCampaign: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: `编辑活动ID: ${id}`,
      icon: 'none'
    });
  },
  
  viewCampaignDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: `查看活动详情ID: ${id}`,
      icon: 'none'
    });
  },
  
  manageCoupon: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: `管理优惠券ID: ${id}`,
      icon: 'none'
    });
  },
  
  viewChannelDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: `查看渠道详情ID: ${id}`,
      icon: 'none'
    });
  },
  
  createNewCampaign: function() {
    wx.showToast({
      title: '创建新活动',
      icon: 'none'
    });
  }
}); 